library("haven")
library("tidyverse")
library("broom")
library("scales")
library("broom")
library("plm")
library("estimatr")


UA <- read_dta("Ukraine_Refugees_Replication.dta")


#######################
#Static Analysis by wave
#######################

summary(UA)

summary(c21 <- lm(
  C2_1 ~ 
    factor(country) +
    #    factor(wave)+ 
    low_income +
    factor(gender) + 
    age + 
    polint + 
    factor(ideology) + 
    EU_ID + 
    B4_4 + 
    A5_2 + 
    pro_ua_us_nato + 
    pro_immigration,
  data = subset(UA, wave ==1)
  , weights = weight
))





summary(c41 <- lm(
  C4_1 ~ 
    factor(country) +
    #    factor(wave)+ 
    low_income +
    factor(gender) + 
    age + 
    polint + 
    factor(ideology) + 
    EU_ID + 
    B4_4 + 
    A5_2 + 
    pro_ua_us_nato + 
    pro_immigration,
  data = subset(UA, wave ==1)
  , weights = weight
))


summary(c22 <- lm(
  C2_2 ~
    factor(country) + 
    #    factor(wave)+ 
    low_income +
    factor(gender) + 
    age + 
    polint + 
    factor(ideology) + 
    EU_ID + 
    B4_4 + 
    A5_2 + 
    pro_ua_us_nato + 
    pro_immigration,
  data = subset(UA, wave ==1)
  , weights = weight
))


summary(c23<- lm(
  C2_3 ~ 
    factor(country) + 
    #    factor(wave)+ 
    low_income +
    factor(gender) + 
    age + 
    polint + 
    factor(ideology) + 
    EU_ID + 
    B4_4 + 
    A5_2 + 
    pro_ua_us_nato + 
    pro_immigration,
  data = subset(UA, wave ==1)
  , weights = weight
))




#####Pooled analysis with fixed effects and clustered standard errors at individual level.


summary(w2war <- lm_robust(C2_1 ~ 
                             factor(country) +
                             pro_ua_us_nato+ 
                             pro_immigration+
                             factor(wave)*pro_ua_us_nato+
                             low_income +
                             factor(gender) + 
                             age + 
                             polint + 
                             factor(ideology) + 
                             EU_ID + 
                             B4_4 + 
                             A5_2,
                           clusters = caseid_merged,
                           data = UA
                           , weights = weight))

summary(w2imm <- lm_robust(C2_1 ~ 
                             factor(country) +
                             pro_ua_us_nato+
                             pro_immigration+
                             factor(wave)*pro_immigration+ 
                             low_income +
                             factor(gender) + 
                             age + 
                             polint + 
                             factor(ideology) + 
                             EU_ID + 
                             B4_4 + 
                             A5_2,
                           clusters = caseid_merged,
                           data = UA
                           , weights = weight))

summary(w2war2 <- lm_robust(C2_2 ~ 
                             factor(country) +
                             pro_ua_us_nato+ 
                             pro_immigration+
                             factor(wave)*pro_ua_us_nato+
                             low_income +
                             factor(gender) + 
                             age + 
                             polint + 
                             factor(ideology) + 
                             EU_ID + 
                             B4_4 + 
                             A5_2,
                           clusters = caseid_merged,
                           data = UA
                           , weights = weight))

summary(w2imm2 <- lm_robust(C2_2 ~ 
                             factor(country) +
                             pro_ua_us_nato+
                             pro_immigration+
                             factor(wave)*pro_immigration+ 
                             low_income +
                             factor(gender) + 
                             age + 
                             polint + 
                             factor(ideology) + 
                             EU_ID + 
                             B4_4 + 
                             A5_2,
                           clusters = caseid_merged,
                           data = UA
                           , weights = weight))



###Appendix tables by country


summary(c21f <- lm(
  C2_1 ~ 
    low_income +
    factor(gender) + 
    age + 
    polint + 
    factor(ideology) + 
    EU_ID + 
    B4_4 + 
    A5_2 + 
    pro_ua_us_nato + 
    pro_immigration,
  data = subset(UA, wave ==1 & country == 2)
  , weights = weight
))



summary(c21d <- lm(
  C2_1 ~ 
    low_income +
    factor(gender) + 
    age + 
    polint + 
    ideology + 
    EU_ID + 
    B4_4 + 
    A5_2 + 
    pro_ua_us_nato + 
    pro_immigration,
  data = subset(UA, wave ==1 & country == 1)
  , weights = weight
))


summary(c21i <- lm(
  C2_1 ~ 
    low_income +
    factor(gender) + 
    age + 
    polint + 
    ideology + 
    EU_ID + 
    B4_4 + 
    A5_2 + 
    pro_ua_us_nato + 
    pro_immigration,
  data = subset(UA, wave ==1 & country == 4)
  , weights = weight
))


summary(c21p <- lm(
  C2_1 ~ 
    low_income +
    factor(gender) + 
    age + 
    polint + 
    ideology + 
    EU_ID + 
    B4_4 + 
    A5_2 + 
    pro_ua_us_nato + 
    pro_immigration,
  data = subset(UA, wave ==1 & country == 5)
  , weights = weight
))


summary(c21h <- lm(
  C2_1 ~ 
    low_income +
    factor(gender) + 
    age + 
    polint + 
    ideology + 
    EU_ID + 
    B4_4 + 
    A5_2 + 
    pro_ua_us_nato + 
    pro_immigration,
  data = subset(UA, wave ==1 & country == 3)
  , weights = weight
))



##########################
####Fixed Effects Analysis
##########################

###Fixed Effects/ Within Model ####

summary(lm_robust(C2_1 ~ 
                    pro_immigration +
                    pro_ua_us_nato+
                    A5_2+  #trust Russia
                    B4_4, #problem pressure
                  clusters = caseid_merged,
                  fixed_effects = ~caseid_merged,
                  se_type= "stata",
                  data = UA
                  , weights = weight))



summary(lm_robust(C2_2 ~ 
                    pro_immigration +
                    pro_ua_us_nato+
                    A5_2+  #trust Russia
                    B4_4, #problem pressure
                  clusters = caseid_merged,
                  fixed_effects = ~caseid_merged,
                  se_type= "stata",
                  data = UA
                  , weights = weight))


summary(lm_robust(C2_3 ~ 
                    pro_immigration +
                    pro_ua_us_nato+
                    A5_2+  #trust Russia
                    B4_4, #problem pressure
                  clusters = caseid_merged,
                  fixed_effects = ~caseid_merged,
                  se_type= "stata",
                  data = UA
                  , weights = weight))


summary(lm_robust(C4_1 ~ 
                    pro_immigration +
                    pro_ua_us_nato+
                    A5_2+  #trust Russia
                    B4_4, #problem pressure
                  clusters = caseid_merged,
                  fixed_effects = ~caseid_merged,
                  se_type= "stata",
                  data = UA
                  , weights = weight))


#####Spill-over analysis####


summary(lm_robust(  C2_2 ~ C2_1 +     UA_first+
                    pro_immigration +
                    pro_ua_us_nato+
                    A5_2+  #trust Russia
                    B4_4, #problem pressure
                  clusters = caseid_merged,
                  fixed_effects = ~caseid_merged,
                  se_type= "stata",
                  data = UA
                  , weights = weight))


summary(lm_robust(  C2_3 ~ C2_1 +     UA_first+
                      pro_immigration +
                      pro_ua_us_nato+
                      A5_2+  #trust Russia
                      B4_4, #problem pressure
                    clusters = caseid_merged,
                    fixed_effects = ~caseid_merged,
                    se_type= "stata",
                    data = UA
                    , weights = weight))




###Robustness for spillover effect


summary(lm_robust(  C2_2 ~ C2_1 +
                      pro_immigration +
                      pro_ua_us_nato+
                      A5_2+  #trust Russia
                      B4_4, #problem pressure
                    clusters = caseid_merged,
                    fixed_effects = ~caseid_merged,
                    se_type= "stata",
                    data=subset(UAp, UA_first==0)
                    , weights = weight))




summary(lm_robust(  C2_3 ~ C2_1 +
                      pro_immigration +
                      pro_ua_us_nato+
                      A5_2+  #trust Russia
                      B4_4, #problem pressure
                    clusters = caseid_merged,
                    fixed_effects = ~caseid_merged,
                    se_type= "stata",
                    data=subset(UAp, UA_first==0)
                    , weights = weight))



######################
###Dynamic Analysis###
######################

#Creating Change variables

#C2_1
UA_panel <- UA %>%
  select(caseid_merged,wave_cat,C2_1) %>% 
  spread(key=wave_cat, value=C2_1) %>% 
  mutate(C2_1_diff = wave2-wave1) %>% 
  select(-wave1, -wave2)

#C2_2
UA_c22 <- UA %>%
  select(caseid_merged,wave_cat,C2_2) %>% 
  spread(key=wave_cat, value=C2_2) %>% 
  mutate(C2_2_diff = wave2-wave1) %>% 
  select(-wave1, -wave2)

UA_panel <- left_join(UA_panel, UA_c22, by="caseid_merged")

#C2_3
UA_c23 <- UA %>%
  select(caseid_merged,wave_cat,C2_3) %>% 
  spread(key=wave_cat, value=C2_3) %>% 
  mutate(C2_3_diff = wave2-wave1) %>% 
  select(-wave1, -wave2)

UA_panel <- left_join(UA_panel, UA_c23, by="caseid_merged")


#C4_1
UA_c41 <- UA %>%
  select(caseid_merged,wave_cat,C4_1) %>% 
  spread(key=wave_cat, value=C4_1) %>% 
  mutate(C4_1_diff = wave2-wave1) %>% 
  select(-wave1, -wave2)

UA_panel <- left_join(UA_panel, UA_c41, by="caseid_merged")


#pro_immigration
UA_pi <- UA %>%
  select(caseid_merged,wave_cat,pro_immigration) %>% 
  spread(key=wave_cat, value=pro_immigration) %>% 
  mutate(pro_immigration_diff = wave2-wave1) %>% 
  select(-wave1, -wave2)

UA_panel <- left_join(UA_panel, UA_pi, by="caseid_merged")


#pro_ua_us_nato
UA_nato <- UA %>%
  select(caseid_merged,wave_cat,pro_ua_us_nato) %>% 
  spread(key=wave_cat, value=pro_ua_us_nato) %>% 
  mutate(pro_ua_us_nato_diff = wave2-wave1) %>% 
  select(-wave1, -wave2)

UA_panel <- left_join(UA_panel, UA_nato, by="caseid_merged")



#A5_2

UA_52 <- UA %>%
  select(caseid_merged,wave_cat,A5_2) %>% 
  spread(key=wave_cat, value=A5_2) %>% 
  mutate(A5_2_diff = wave2-wave1) %>% 
  select(-wave1, -wave2)

UA_panel <- left_join(UA_panel, UA_52, by="caseid_merged")


#B4_4

UA_b44 <- UA %>%
  select(caseid_merged,wave_cat,B4_4) %>% 
  spread(key=wave_cat, value=B4_4) %>% 
  mutate(B4_4_diff = wave2-wave1) %>% 
  select(-wave1, -wave2)

UA_panel <- left_join(UA_panel, UA_b44, by="caseid_merged")

#EU_ID

UA_EUID <- UA %>%
  select(caseid_merged,wave_cat,EU_ID) %>% 
  spread(key=wave_cat, value=EU_ID) %>% 
  mutate(EU_ID_diff = wave2-wave1) %>% 
  select(-wave1, -wave2)

UA_panel <- left_join(UA_panel, UA_EUID, by="caseid_merged")


rm(UA_52,UA_b44,UA_c22,UA_c23,UA_c41,UA_EUID,UA_pi,UA_nato)

###Creating level variables
UA_wave1 <- filter(UA, wave==1)

#rename variables to level

UA_wave1 <- UA_wave1 %>% 
  dplyr::select(caseid_merged, country, C2_1, C2_2, C2_3, C4_1, pro_immigration,pro_ua_us_nato, A5_2,B4_4, EU_ID, polint, age, low_income, gender, ideology) %>% 
  rename(C2_1_level = C2_1, C2_2_level = C2_2, C2_3_level = C2_3, C4_1_level = C4_1, pro_immigration_level = pro_immigration,pro_ua_us_nato_level = pro_ua_us_nato, A5_2_level = A5_2,B4_4_level = B4_4, EU_ID_level = EU_ID)



###Merging#####

UA_panel <- left_join(UA_panel, UA_wave1, by = "caseid_merged")

rm(UA_wave1, UA_wave2, UA)

#RESCALE all

UA_panel <- UA_panel %>% 
  mutate(C2_1_diff = rescale(C2_1_diff),
         C2_1_level = rescale(C2_1_level),
         C2_2_diff = rescale(C2_2_diff),
         C2_2_level = rescale(C2_2_level),
         C2_3_diff = rescale(C2_3_diff),
         C2_3_level = rescale(C2_3_level),
         C4_1_diff = rescale(C4_1_diff),
         C4_1_level = rescale(C4_1_level),
         pro_immigration_diff = rescale(pro_immigration_diff),
         pro_immigration_level = rescale(pro_immigration_level),
         pro_ua_us_nato_diff = rescale(pro_ua_us_nato_diff),
         pro_ua_us_nato_level = rescale(pro_ua_us_nato_level),
         A5_2_diff = rescale(A5_2_diff),
         A5_2_level = rescale(A5_2_level),
         B4_4_diff = rescale(B4_4_diff),
         B4_4_level = rescale(B4_4_level),
         EU_ID_diff = rescale(EU_ID_diff),
         EU_ID_level = rescale(EU_ID_level),
         polint = rescale(as.numeric(polint)),
         age = rescale(age),
         low_income = rescale(low_income)
  )


levels(UA_panel$country) <- c("Germany","France","Hungary","Italy","Poland","6","7")

#write_dta(UA_panel, "UA_diff.dta")






######Analysis ##############

#UA_panel <- read_dta("UA_diff.dta")

#####Continuous OUTCOME Variables

summary(diff_c21 <- lm(
  C2_1_diff ~ C2_1_level+
    pro_immigration_diff + pro_immigration_level + 
    pro_ua_us_nato_diff+ pro_ua_us_nato_level+
    A5_2_diff+ A5_2_level+#trust Russia
    B4_4_diff + B4_4_level+ #problem pressure
    EU_ID_diff+EU_ID_level+
    ideology+
    polint + #pol interest
    age + factor(gender) + low_income+#demographics
    country, data=UA_panel
))

summary(diff_c22 <- lm(
  C2_2_diff ~ C2_2_level+
    pro_immigration_diff + pro_immigration_level + 
    pro_ua_us_nato_diff+ pro_ua_us_nato_level+
    A5_2_diff+ A5_2_level+#trust Russia
    B4_4_diff + B4_4_level+ #problem pressure
    EU_ID_diff+EU_ID_level+
    ideology+
    polint+#pol interest
    age + factor(gender) + low_income+#demographics
    country, data=UA_panel
))



summary(diff_c23 <- lm(
  C2_3_diff ~ C2_3_level+
    pro_immigration_diff + pro_immigration_level + 
    pro_ua_us_nato_diff+ pro_ua_us_nato_level+
    A5_2_diff+ A5_2_level+#trust Russia
    B4_4_diff + B4_4_level+ #problem pressure
    EU_ID_diff+EU_ID_level+
    ideology+
    polint+#pol interest
    age + factor(gender) + low_income+#demographics
    country, data=UA_panel
))


summary(diff_c41 <- lm(
  C4_1_diff ~ C4_1_level+
    pro_immigration_diff + pro_immigration_level + 
    pro_ua_us_nato_diff+ pro_ua_us_nato_level+
    A5_2_diff+ A5_2_level+#trust Russia
    B4_4_diff + B4_4_level+ #problem pressure
    EU_ID_diff+EU_ID_level+
    ideology+
    polint+#pol interest
    age + factor(gender) + low_income+#demographics
    country, data=UA_panel
))



